package com.testshiro.aspect;


import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import com.testshiro.tools.DynamicDataSourceHolder;
import com.testshiro.utils.DataSource;

@Aspect
public class DataSourceAspect {
	private final static Logger log = Logger.getLogger(DataSourceAspect.class);
    
    
	@Before("execution(* com.testshiro.service..*.*(..))&&@annotation(ds)")
    public void changeDataSource(JoinPoint point, DataSource ds) throws Throwable {
		String key = ds.value();
        if(key!=null && !key.trim().equals("")){
        	DynamicDataSourceHolder.setDataSourceType(key);
        }
    }

    @After("execution(* com.testshiro.service..*.*(..))&&@annotation(ds)")
    public void restoreDataSource(JoinPoint point, DataSource ds) {
    	DynamicDataSourceHolder.clearDataSourceType();
    }
}
